Memory

Large Object Heap (LOH)

  • Objects larger than ~85 KB go into the Large Object Heap.

  • This affects performance-sensitive code.

  • Ex:

    byte[] buffer = new byte[100000];
    

GC Phases

  • mark reachable objects

  • sweep unreachable objects

  • compact memory

  • Generational collection order:

    • Gen0 → Gen1 → Gen2

  • Most collections only touch Gen0.

Pinning

  • Sometimes memory must not move during GC.

  • Pinned objects cannot be relocated.

  • Too many pinned objects cause heap fragmentation.

fixed (byte* p = buffer)
{
}